﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Ftp
{
    /// <summary>
    /// 返回指定文件的最后修改时间
    /// RFC 3659	Return the last-modified time of a specified file.
    /// </summary>
    internal class FtpMDTMCommand : FtpCommandAbs
    {
        /***********************************************************************************************************
        *
        *
        ************************************************************************************************************/


        public FtpMDTMCommand()
            : base()
        {

        }

        protected override string[] GetNames()
        {
            return new string[] { FtpCommands.MDTM };
        }

        protected override IFtpResponse PrimitiveExcute(FtpSession session, string command, string commandLine)
        {
            //Request: MDTM /FontsIcon.rar
            //Response: 213 20220318080240

            try
            {
                string fileName = commandLine;
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return new FtpCodeResponse(FtpStatusCodes.Code501, "The parameter must specify a file name");
                }

                string filePath = session.GetLocalPath(fileName);
                if (File.Exists(filePath))
                {
                    DateTime lastModifyTime = File.GetLastWriteTimeUtc(filePath);
                    return new FtpCodeResponse(FtpStatusCodes.Code213, lastModifyTime.ToString("yyyyMMddHHmmss"));
                }
                else
                {
                    return new FtpCodeResponse(FtpStatusCodes.Code550, "not found file.");
                }
            }
            catch (Exception ex)
            {
                return new FtpCodeResponse(FtpStatusCodes.Code451, ex.Message);
            }
        }
    }

}
